package com.iltgcl.echovoicedemo.utils;

import android.content.Context;

import androidx.annotation.XmlRes;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceGroup;

import java.util.HashSet;
import java.util.Set;

public class PreferenceSettingsUtils {
    // Cannot be instantiated.
    private PreferenceSettingsUtils() {}

    /**
     * Given an array of resource ids corresponding to the actual String keys for TalkBack
     * preferences, hide the preferences. This will also be done recursively form the root
     * PreferenceGroup.
     */
    public static void hidePreferences(
            final Context context, PreferenceGroup root, int[] prefKeyIds) {
        if (context == null) {
            return;
        }
        Set<String> hiddenPreferenceKeys = new HashSet<>();
        for (int hiddenPreferenceKeyId : prefKeyIds) {
            hiddenPreferenceKeys.add(context.getString(hiddenPreferenceKeyId));
        }
        hidePreferences(root, hiddenPreferenceKeys);
    }

    /**
     * Given the resource id corresponding to the actual String key for TalkBack preference, hide the
     * preference. This will also be done recursively form the root PreferenceGroup.
     */
    public static void hidePreference(final Context context, PreferenceGroup root, int prefKeyId) {
        hidePreferences(context, root, new int[] {prefKeyId});
    }

    private static void hidePreferences(PreferenceGroup root, Set<String> preferenceKeysToBeHidden) {
        for (int i = 0; i < root.getPreferenceCount(); i++) {
            Preference preference = root.getPreference(i);
            if (preferenceKeysToBeHidden.contains(preference.getKey())) {
                root.removePreference(preference);
                i--;
            } else if (preference instanceof PreferenceGroup) {
                hidePreferences((PreferenceGroup) preference, preferenceKeysToBeHidden);
            }
        }
    }

    /**
     * Inflates the given XML resource and replaces the current preference hierarchy (if any) with the
     * preference hierarchy rooted at {@code key}.{@link
     * PreferenceFragmentCompat#setPreferencesFromResource(int, String)}
     */
    public static void setPreferencesFromResource(
            PreferenceFragmentCompat preferenceFragment, @XmlRes int preferencesResId, String key) {
        // Set preferences to use device-protected storage.
        if (BuildVersionUtils.isAtLeastN()) {
            preferenceFragment.getPreferenceManager().setStorageDeviceProtected();
        }
        preferenceFragment.setPreferencesFromResource(preferencesResId, key);
    }

    /**
     * Inflates the given XML resource and adds the preference hierarchy to the current preference
     * hierarchy. {@link PreferenceFragmentCompat#addPreferencesFromResource}
     */
    public static void addPreferencesFromResource(
            PreferenceFragmentCompat preferenceFragment, @XmlRes int preferencesResId) {
        // Set preferences to use device-protected storage.
        if (BuildVersionUtils.isAtLeastN()) {
            preferenceFragment.getPreferenceManager().setStorageDeviceProtected();
        }
        preferenceFragment.addPreferencesFromResource(preferencesResId);
    }

}
